package com.tiennt.android.smartcaller;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;

import com.tiennt.android.smartcaller.ui.IOnLeftRightChoice;
import com.tiennt.android.smartcaller.ui.SlidingTab;
import com.tiennt.android.smartcaller.utils.ContactUtils;
import com.tiennt.android.smartcaller.utils.SmartCallerUtils;

public class IncomingCallActivity extends Activity implements
		IOnLeftRightChoice {

	@Override
	public void onLeftRightChoice(int whichHandle) {
		if (whichHandle == IOnLeftRightChoice.LEFT_HANDLE) {
			// Answer call
//            mCallback.goToUnlockScreen();
			Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
        } else if (whichHandle == IOnLeftRightChoice.RIGHT_HANDLE) {
        	// Decline call
        	Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
        }

	}

	private SlidingDrawer slidingDrawer;
	private TextView txtContactName;
	private ImageView imgContactImage;
	private SlidingTab stab;
	IOnLeftRightChoice onLRChoiceListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.incoming_call_screen);

		init();
	}

	private void init() {
		// Init Sliding drawer
		slidingDrawer = (SlidingDrawer) findViewById(R.id.drawerContactInfo);
		txtContactName = (TextView) findViewById(R.id.txtIncomingContact);
		imgContactImage = (ImageView) findViewById(R.id.imgViewContactImage);
		stab = (SlidingTab) findViewById(R.id.slidingTab);

		setEventHandler();
		setComponentResource();

	}

	private void setEventHandler() {
		final ImageView imgView = (ImageView) findViewById(R.id.handle);
		slidingDrawer
				.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {

					@Override
					public void onDrawerOpened() {
						// TODO Auto-generated method stub
						imgView.setImageResource(R.drawable.close);
					}
				});
		slidingDrawer
				.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {

					@Override
					public void onDrawerClosed() {
						// TODO Auto-generated method stub
						imgView.setImageResource(R.drawable.open);
					}
				});
	}

	private void setComponentResource() {
		// Sliding Tab:
		stab.setLeftHintText(R.string.answer);

		stab.setLeftTabResources(R.drawable.ic_jog_dial_answer,
				R.drawable.jog_tab_target_green,
				R.drawable.jog_tab_bar_left_answer,
				R.drawable.jog_tab_left_answer);

		stab.setRightHintText(R.string.reject);
//		stab.setOnLeftRightListener(onLRChoiceListener);

		// Contact Image && Contact Name:
		Bundle bundle = getIntent().getExtras();
		String phoneNumber = bundle
				.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

		SmartCallerUtils.log("Phone NUM: " + phoneNumber);

		Uri contackLookup = Uri.withAppendedPath(
				ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(phoneNumber));

		Cursor cursor = getContentResolver().query(
				contackLookup,
				new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME,
						ContactsContract.PhoneLookup._ID,
						ContactsContract.PhoneLookup.PHOTO_ID }, null, null,
				null);

		String contactId = "";
		String contactName = phoneNumber;
		String contactPhotoId = "";
		if (cursor.moveToFirst()) {
			
			// Get Contact Id and Photo Id
			contactId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.PhoneLookup._ID));
			contactName = cursor.getString(cursor
					.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
			contactPhotoId = cursor.getString(cursor
					.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_ID));
			SmartCallerUtils.log("Photo ID: " + contactPhotoId);

			// Set bitmap for contact image
			Bitmap contactBitmap = ContactUtils.loadContactPhoto(
					getContentResolver(), contactId, contactPhotoId);
			if (contactBitmap != null) {
				imgContactImage.setImageBitmap(contactBitmap);
			}
		}

		txtContactName.setText(contactName);
	}

}
